﻿using System;
using Bricks.Injecting;
using Microsoft.Practices.Unity;
using Motar.UnityExtensions.Properties;

namespace Motar.UnityExtensions
{

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TExtension"></typeparam>
    public class UnityBootstrapper<TExtension> : BootstrapperBase where TExtension : UnityBootstrapperExtension
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="unityContainerAccessor"></param>
        public UnityBootstrapper(Func<IUnityContainer> unityContainerAccessor) :
            base(() =>
            {
                IUnityContainer container = unityContainerAccessor.Invoke();

                if (container == null)
                {
                    throw new InvalidOperationException(Resources.NullUnityContainerException);
                }

                container.AddNewExtension<TExtension>();

                return container.Resolve<IContainerFacade>();
            })
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public UnityBootstrapper() :
            this(() => new UnityContainer())
        {
        }
    }
}
